package dimensional.traveler.client.screens.cinematic;

import com.google.gwt.user.client.ui.Image;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.core.client.util.Padding;
import com.sencha.gxt.widget.core.client.Composite;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutData;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer.HBoxLayoutAlign;
import com.sencha.gxt.widget.core.client.form.TextArea;

import dimensional.traveler.shared.Settings;
import dimensional.traveler.shared.model.ConversationLine;

public class ConversationWidget extends Composite {

	private static final String PORTRAITS_PATH = "/images/portraits/";
	private static final String PORTRAIT_PLACEHOLDER = PORTRAITS_PATH + "/placeholder.jpg";

	public ConversationWidget(ConversationLine conversationLine) {
		TextArea text = createText(conversationLine);
		Image portrait = createPortrait(conversationLine);

	    HBoxLayoutContainer c = new HBoxLayoutContainer();
        c.setPadding(new Padding(5));
        c.setHBoxLayoutAlign(HBoxLayoutAlign.MIDDLE);
        c.setPack(BoxLayoutPack.END);

        BoxLayoutData layoutData = new BoxLayoutData(new Margins(0, 5, 0, 0));
        c.add(text, layoutData);
        c.add(portrait, layoutData);
        initWidget(c);
	}

	private TextArea createText(ConversationLine conversationLine) {
		TextArea text = new TextArea();
		text.setValue(conversationLine.line);
		return text;
	}

	private Image createPortrait(ConversationLine conversationLine) {
		Image portrait = new Image(PORTRAIT_PLACEHOLDER);
		portrait.setPixelSize(100, 120);
		if (!Settings.USE_PLACEHOLDERS)
			portrait.setUrl(PORTRAITS_PATH + conversationLine.characterId + ".jpg");
		return portrait;
	}
}
